//
//  MissileEnemy.m
//  iFly
//
//  Created by User-10 on 12/7/14.
//  Copyright (c) 2014 TeamDifferent. All rights reserved.
//

#import "MissileEnemy.h"
#import "Constants.h"

@implementation MissileEnemy


+(instancetype)missileWithPosition:(CGPoint)position andHealth:(NSInteger)health andPointsForKill:(NSInteger)points {
    
    MissileEnemy* missile = [super enemyWithImageNamed:@"red-rocket.png" andPosition:position andHealth:health andPointsForKill:points];

    return missile;
}

-(void)moveToPosition:(CGPoint)position {
    // Shooting the missile at the ship
    CGPoint location = position;
    // Determine offset of location to projectile
    CGPoint offset = [VectorHelper vectorSubPoint:location toPoint:self.position];
    
    // Get the direction of where to shoot
    CGPoint direction = [VectorHelper vectorNormalize:offset];
    
    // Make it shoot far enough to be guaranteed off screen
    CGPoint shootAmount = [VectorHelper vectorMultPoint:direction withMultiplier:outOfScreenOffset];
    
    // Add the shoot amount to the current position
    CGPoint realDest = [VectorHelper vectorAddPoint:shootAmount toPoint:self.position];
    
    int minDuration = missileMinSpeed;
    int maxDuration = missileMaxSpeed;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = (arc4random() % rangeDuration) + minDuration;
    
    self.zRotation = -direction.y;
    
    SKAction* move = [SKAction moveTo:realDest duration:actualDuration];
    SKAction* moveDone = [SKAction removeFromParent];
    [self runAction:[SKAction sequence:@[move, moveDone]]];

}

@end
